package kr.co.goodwilldd.controller.common;

import java.util.Collections;

import kr.co.goodwilldd.entity.menus.model.MenuEntity;
import kr.co.goodwilldd.entity.restful.model.ChildEntity;
import kr.co.goodwilldd.entity.restful.model.RestEntity;
import kr.co.goodwilldd.service.menus.IMenuService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "rest")
public class RESTfulController {

	@Autowired
	IMenuService iMenuService;

	@RequestMapping(value = "getMenu")
	public @ResponseBody
	RestEntity<MenuEntity> getMenu() {
		RestEntity<MenuEntity> result = new RestEntity<MenuEntity>();
		try {
			result.setCode("0");
			result.setResult("success");
			result.setDatas(Collections.singletonList(iMenuService.getTree()));			
		} catch (Exception e) {
			result.setCode("1");
			result.setResult("failure");
		}
		return result;
	}
	
	@RequestMapping(value = "getMenus")
	public @ResponseBody
	ResponseEntity<String> getMenus() {
		return new ResponseEntity<String>(iMenuService.getTree2(), HttpStatus.OK);
	}
	
	@RequestMapping(value = "getMenuSequence")
	public @ResponseBody
	ResponseEntity<String> getMenuSequence() {
		return new ResponseEntity<String>(iMenuService.getMenuSequence(), HttpStatus.OK);
	}
	
	@RequestMapping(value = "doUpdateMenu")
	public @ResponseBody ResponseEntity<String> doUpdateMenu(@RequestParam(value = "data") String data) {
		try {
			iMenuService.doUpdateMenu(data);
		} catch (Exception e) {
			e.printStackTrace();
			return new ResponseEntity<String>("false", HttpStatus.OK);	
		}
		return new ResponseEntity<String>("success", HttpStatus.OK);
	}

	@RequestMapping("/entity")
	public @ResponseBody
	RestEntity<ChildEntity> entity(Model model) {
		ChildEntity child = new ChildEntity();
		child.setSubject("subject");
		// child.setWriter("writer");
		RestEntity<ChildEntity> result = new RestEntity<ChildEntity>();
		result.setDatas(Collections.singletonList(child));
		result.setCode("code");
		result.setResult("result");
		return result;
	}
	
	@Secured(value = {"ROLE_SUPERVISOR"})
	@RequestMapping("siteInit")
	public String siteInit() {
		return null;
	}
}
